package com.hwg.emp.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.hwg.emp.common.dto.KeyQueryDto;
import com.hwg.emp.common.util.DBUtil;
import com.hwg.emp.dao.UserDAO;
import com.hwg.emp.model.UserMD;

public class UserDAOImpl implements UserDAO {
    
	/**
	 * 增加用户
	 * @return 
	 * @return 
	 */
	@Override
	public int insert(UserMD t) throws Exception {
		String sql = "INSERT INTO sys_user (id,account,password,name) VALUES (?,?,?,?)";
		Connection con = DBUtil.getConnection();
		PreparedStatement ps = con.prepareStatement(sql);
		ps.setString(1, t.getId());
		ps.setString(2, t.getAccount());
		ps.setString(3, t.getPassword());
		ps.setString(4, t.getName());
		int i = ps.executeUpdate();
		return i;
	}

	@Override
	public int delete(String uk, String ukValue) throws Exception {
		Connection con=DBUtil.getConnection();
		StringBuilder sql=new StringBuilder();
		sql.append("delete  from sys_user where ")
		.append(uk).append("=?");
		PreparedStatement ps=con.prepareStatement(sql.toString());
		ps.setObject(1, ukValue);
		return ps.executeUpdate();
		
	}
    
	/**
	 * 修改用户密码
	 * 传入用户对象
	 */
	@Override
	public int update(UserMD t) throws Exception {
		
		String sql = "update sys_user set password=?,account=?,name=? where id=?";
		Connection con=DBUtil.getConnection();
		PreparedStatement ps=con.prepareStatement(sql);
		ps.setString(1, t.getPassword());
		ps.setString(2, t.getAccount());
		ps.setString(3, t.getName());
		ps.setString(4, t.getId());
		int i = ps.executeUpdate();
		return i;
	}

	@Override
	public UserMD select(String uk, String ukValue) throws Exception {
		Connection con=DBUtil.getConnection();
		StringBuilder sql=new StringBuilder();
		sql.append("select * from sys_user where ")
		.append(uk).append("=?");
		PreparedStatement ps=con.prepareStatement(sql.toString());
		ps.setObject(1, ukValue);
		ResultSet rs=ps.executeQuery();
		if(rs.next()){
			return result2dm(rs);
		}
		return null;
	}
	
	/**
	 * 拼凑结果集
	 * @param rs 传入一个结果集
	 * @return 用户对象
	 * @throws SQLException
	 */
	private UserMD result2dm(ResultSet rs)throws SQLException{
		UserMD dm=new UserMD();
		dm.setAccount(rs.getString("account"));
		dm.setId(rs.getString("id"));
		dm.setName(rs.getString("name"));
		dm.setPassword(rs.getString("password"));
		return dm;
	}

	@Override
	public boolean exist(String uk, String ukValue) throws Exception {
		Connection con=DBUtil.getConnection();
		StringBuilder sql=new StringBuilder();
		sql.append("select count(*) from sys_user where ")
		.append(uk).append("=?");
		PreparedStatement ps=con.prepareStatement(sql.toString());
		ps.setObject(1, ukValue);
		ResultSet rs=ps.executeQuery();
		if(rs.next()){
			return rs.getInt(1)>0;
		}
		return false;
	}

	@Override
	public int selectByKey(KeyQueryDto dto, List<UserMD> pageData)
			throws Exception {
		return 0;
	}

	@Override
	public int updateUserOnfo(UserMD t) throws Exception {
		// TODO Auto-generated method stub
		String sql = "update sys_user set ,account=?,name=? where id=?";
		Connection con=DBUtil.getConnection();
		PreparedStatement ps=con.prepareStatement(sql);
		ps.setString(1, t.getAccount());
		ps.setString(2, t.getName());
		ps.setString(3, t.getId());
		int i = ps.executeUpdate();
		return i;
	}
}
